/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PictureID;

import elsimulatorjava.Floor;
import elsimulatorjava.Person;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;

/**
 *
 * @author plitter
 */
public class GridPictureID extends JPanel {
    ArrayList<PictureID> peoplePictures;
    ArrayList<Person> people;
    JPanel peopleWaiting;
    JPanel peopleFinished;
    
    public GridPictureID(ArrayList<Person> people) throws IOException {
        this.people = people;
        peoplePictures = new ArrayList<PictureID>();
        
        peopleWaiting = new JPanel(new GridLayout(0,4));
        peopleFinished = new JPanel(new GridLayout(0, 4));

        for (Person person : people) {
            if (person.isFinished()) {
                peoplePictures.add(new PictureID(person));
                peopleFinished.add(peoplePictures.get(peoplePictures.size()-1));
            }
            else {
                peoplePictures.add(new PictureID(person));
                peopleWaiting.add(peoplePictures.get(peoplePictures.size()-1));
            }
        }
        
        
        JScrollPane scroller2 = new JScrollPane(peopleFinished);
        JScrollPane scroller = new JScrollPane(peopleWaiting);
        
        scroller.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("People waiting"),
            BorderFactory.createEmptyBorder(5,5,5,5)));
        scroller2.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("People finished"),
            BorderFactory.createEmptyBorder(5,5,5,5)));
        
        Dimension panelDimension = new Dimension(new Dimension(250,200));
        
        panelDimension.setSize(panelDimension.width+50, 200);
        scroller.setPreferredSize(new Dimension(panelDimension));
        scroller2.setPreferredSize(new Dimension(panelDimension));
        setLayout(new GridLayout(0, 2));
        add(scroller);
        add(scroller2);
        setPreferredSize(new Dimension(panelDimension.width*2,panelDimension.height+50));
    }
    
    public void update() throws IOException {
        peopleWaiting.removeAll();
        peopleFinished.removeAll();
        for (Person person : people) {
            if (person.isFinished()) {
                peoplePictures.add(new PictureID(person));
                peopleFinished.add(peoplePictures.get(peoplePictures.size()-1));
            }
            else {
                peoplePictures.add(new PictureID(person));
                peopleWaiting.add(peoplePictures.get(peoplePictures.size()-1));
            }
        }
    }
    
    public void update(ArrayList<Person> people) throws IOException {
        this.people = people;
        peoplePictures.clear();
        peopleWaiting.removeAll();
        peopleFinished.removeAll();
        for (Person person : people) {
            if (person.isFinished()) {
                peoplePictures.add(new PictureID(person));
                peopleFinished.add(peoplePictures.get(peoplePictures.size()-1));
            }
            else {
                peoplePictures.add(new PictureID(person));
                peopleWaiting.add(peoplePictures.get(peoplePictures.size()-1));
            }
        }
    }
    
    public static void main(String[] args) throws IOException, InterruptedException {
        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout(0, 1));
        ArrayList<Person> people = new ArrayList<Person>();
        
        GridPictureID test = new GridPictureID(people);
        
        
        frame.add(test);
        frame.setVisible(true);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        System.out.println("test");
        for (short i = 0; i < 20; i++) {
            people.add(new Person(i));
        }
        for (short i = 0; i < 20; i++) {
            people.add(new Person(i));
            people.get(i+20).setFinished();
        }
        System.out.println("test");
        test.update(people);
        System.out.println("test");
    }
}
